// import React, { useState } from 'react';
// import styles from './index.less';

// export default (props) => {
//     const [num, setNum] = useState(0);

//     return(
//         <div>

//         </div>
//     )
// }
function PromiseCustom(fn) {
  let status = 'pending';
  let value = '';
  let onResolvedCallback;
  let onRejectedCallback;

  function resolve(data) {
    status = 'fulfilled';
    value = data;
    toDoThen(onResolvedCallback, onRejectedCallback);
  }

  function reject(data) {
    status = 'rejected';
    value = data;
    toDoThen(onResolvedCallback, onRejectedCallback);
  }

  function toDoThen(onFulfill, onReject) {
    if (status === 'fulfilled') {
      onFulfill && onFulfill(value);
      status = 'pending';
    } else if (status === 'rejected') {
      onReject && onReject(value);
      status = 'pending';
    } else {
      onResolvedCallback = onFulfill;
      onRejectedCallback = onReject;
    }
  }

  this.then = function (onFulfill, onReject) {
    toDoThen(onFulfill, onReject);
  };

  fn(resolve, reject);
  return this;
}

export default PromiseCustom;
